package com.jd.system.controller;

import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.PageList;
import com.jd.system.domain.Dictionarytype;
import com.jd.system.service.DictionarytypeService;
import com.jd.system.query.DictionarytypeQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionarytype")
public class DictionarytypeController {

    @Autowired
    private DictionarytypeService dictionarytypeService;

    @GetMapping
    public List<Dictionarytype> loadAll() {
        return dictionarytypeService.loadAll();
    }

    @GetMapping("/{id}")
    public Dictionarytype loadById(@PathVariable("id")Long id) {
        return dictionarytypeService.loadById(id);
    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id")Long id) {
        try {
            dictionarytypeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }

    }

    @PutMapping
    public AjaxResult addUpdate(@RequestBody Dictionarytype dictionarytype) {
        try {
            if (dictionarytype.getId() == null) {
                dictionarytypeService.save(dictionarytype);
            }else {
                dictionarytypeService.update(dictionarytype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }

    }

    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids) {
        try {
            dictionarytypeService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    @PostMapping
    public PageList<Dictionarytype> find(@RequestBody DictionarytypeQuery dictionarytypeQuery) {
        return dictionarytypeService.queryPage(dictionarytypeQuery);
    }



}
